#include "contentmanager.h"

ContentManager::ContentManager(void) {
    this->fonts = new map<const char*, CFont*>();
    this->models = new map<const char*, CMs3dModel*>();
    this->tex480 = NULL;
    this->tex496 = NULL;
    this->textures = new map<const char*, CTexture*>();
    this->textureHit = new map<CTexture*, int>();

}

ContentManager::~ContentManager(void) {
    for (map<const char*, CFont*>::const_iterator it = this->fonts->begin(); it != this->fonts->end(); ++it) {
        delete it->second;
    }

    this->fonts->clear();
    delete this->fonts;

    for (map<const char*, CMs3dModel*>::const_iterator it = this->models->begin(); it != this->models->end(); ++it) {
        delete it->second;
    }

    this->models->clear();
    delete this->models;

    for (map<const char*, CTexture*>::const_iterator it = this->textures->begin(); it != this->textures->end(); ++it) {
        delete it->second;
    }

    this->textures->clear();
    delete this->textures;

    this->textureHit->clear();
    delete this->textureHit;
}

CFont* ContentManager::LoadFont(const char* filename) {
    map<const char*, CFont*>::const_iterator it = this->fonts->find(filename);
    if (it != this->fonts->end()) {
        return (CFont*)(*it).second;
    }

    CFont* font = new CFont();
    font->Load(filename, true);

    this->fonts->insert(pair<const char*, CFont*>(filename, font));
    return font;
}

CMs3dModel* ContentManager::LoadModel(const char* filename) {
    map<const char*, CMs3dModel*>::const_iterator it = this->models->find(filename);
    if (it != this->models->end()) {
        return (CMs3dModel*)(*it).second;
    }

    CMs3dModel* model = new CMs3dModel();
    model->LoadModelData(filename, false);

    this->models->insert(pair<const char*, CMs3dModel*>(filename, model));
    return model;
}

CTexture* ContentManager::LoadTexture(const char* filename) {
    map<const char*, CTexture*>::const_iterator it = this->textures->find(filename);
    if (it != this->textures->end()) {
        return (CTexture*)(*it).second;
    }

    CTexture* texture = new CTexture();
    texture->LoadBitmap(filename);

    this->textures->insert(pair<const char*, CTexture*>(filename, texture));
    return texture;
}

void ContentManager::UseTexture(CTexture* texture) {
    map<CTexture*, int>::iterator it = this->textureHit->find(texture);
    if (it != this->textureHit->end()) {
        (*it).second++;
    } else {
        this->textureHit->insert(pair<CTexture*, int>(texture, 1));
    }

    if (this->tex480 == NULL) {
        this->tex480 = texture;
        this->tex480->Upload(TEXBUF480);
        this->tex480->Select();
    } else if (this->tex496 == NULL) {
        this->tex496 = texture;
        this->tex496->Upload(TEXBUF496);
        this->tex496->Select();
    } else if (this->tex480 == texture || this->tex496 == texture) {
        texture->Select();
    } else {
        it = this->textureHit->find(tex480);
        int tex480hit = (*it).second;
        it = this->textureHit->find(tex496);
        int tex496hit = (*it).second;
        if (tex480hit < tex496hit) {
            this->tex480 = texture;
            this->tex480->Upload(TEXBUF480);
            this->tex480->Select();
        } else {
            this->tex496 = texture;
            this->tex496->Upload(TEXBUF496);
            this->tex496->Select();
        }
    }
}
